package com.wellsoft.mobileoa;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import com.wellsoft.mobileoa.R;
import com.wellsoft.mobileoa.utils.BaseActivity;
import com.wellsoft.mobileoa.utils.SystemConfig;
import com.wmobile.utils.PopConfirmDialog;
import com.wmobile.utils.ServiceResponse;

public class LoginConfigPopDialog extends Dialog {
	BaseActivity baseActivity;
	EditText serverUrlEt;
	CheckBox isRememberUserNameCb;
	CheckBox isRememberPasswordCb;
	LinearLayout exitLl;
	ImageButton exitBgIbn;
	boolean isEdited = false;;

	int dialogResult;
	public static int DialogResultOK = 1;
	public static int DialogResultCANCEL = 0;

	public int getDialogResult() {
		return dialogResult;
	}

	public LoginConfigPopDialog(BaseActivity baseActivity) {
		super(baseActivity, R.style.popDialogTheme);
		this.baseActivity = baseActivity;
		onCreate();
	}

	public void onCreate() {
		setContentView(R.layout.loginconfigact);

		serverUrlEt = (EditText) findViewById(R.id.loginconfigact_serverurlet);
		serverUrlEt.setText(SystemConfig.readServerUrl());
		serverUrlEt.addTextChangedListener(onTextChanged);

		isRememberUserNameCb = (CheckBox) findViewById(R.id.loginconfigact_rememberusernamecb);
		isRememberUserNameCb.setChecked(SystemConfig.readIsRememberUserName());
		isRememberUserNameCb
				.setOnCheckedChangeListener(onRememberUserNameListener);

		isRememberPasswordCb = (CheckBox) findViewById(R.id.loginconfigact_rememberpasswordcb);
		isRememberPasswordCb.setChecked(SystemConfig.readIsRememberPassword());
		isRememberPasswordCb
				.setOnCheckedChangeListener(onRememberPasswordListener);

		exitBgIbn = (ImageButton) findViewById(R.id.loginconfigact_exitibn);
		exitBgIbn.setOnClickListener(onExitListener);
		exitBgIbn.setOnTouchListener(onExitTouchListener);
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			onExitListener.onClick(null);
		}

		return super.onKeyDown(keyCode, event);
	}

	View.OnClickListener onExitListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			endDialog(0);
		}

	};

	OnTouchListener onExitTouchListener = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				exitBgIbn.setBackgroundColor(Color.argb(127, 255, 255, 255));
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				exitBgIbn.setBackgroundColor(Color.argb(0, 255, 255, 255));
			} else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
				exitBgIbn.setBackgroundColor(Color.argb(0, 255, 255, 255));
			} else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
				exitBgIbn.setBackgroundColor(Color.argb(0, 255, 255, 255));
			}
			return false;
		}
	};

	// OnFocusChangeListener onServerUrlListener = new OnFocusChangeListener() {
	//
	// @Override
	// public void onFocusChange(View v, boolean hasFocus) {
	// if(!hasFocus){
	// isEdited = true;
	// }
	// }
	// };

	public TextWatcher onTextChanged = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {

		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {

		}

		@Override
		public void afterTextChanged(Editable s) {
			isEdited = true;
		}
	};

	OnCheckedChangeListener onRememberUserNameListener = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			isEdited = true;
		}

	};

	OnCheckedChangeListener onRememberPasswordListener = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			isEdited = true;
		}

	};

	public void endDialog(int result) {
		if (isEdited) {
			PopConfirmDialog confirmDialog = new PopConfirmDialog(baseActivity,
					"设置已变更，是否保存？");
			if (confirmDialog.showDialog() != PopConfirmDialog.DialogResultOK) {
				dismiss();
				dialogResult = result;
				return;
			}
			String serverUrl = serverUrlEt.getText().toString();
			if (SystemConfig.stringIsNullOrEmpty(serverUrl)) {
				return;
			}
			SystemConfig.saveServerUrl(serverUrl);

			SystemConfig.saveIsRememberUserName(isRememberUserNameCb
					.isChecked());
			if (!isRememberUserNameCb.isChecked()) {
				SystemConfig.saveRememberedUserName("");
			}

			SystemConfig.saveIsRememberPassword(isRememberPasswordCb
					.isChecked());
			if (!isRememberPasswordCb.isChecked()) {
				SystemConfig.saveRememberedPassword("");
			}
		}
		dismiss();
		dialogResult = result;
	}

}
